Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing Host: header #1579

Closed
wants to merge 9 commits into from
Closed

Handle missing Host: header #1579

wants to merge 9 commits into from

Conversation

SLaks
Copy link

@SLaks SLaks commented Apr 15, 2013

Right now, a request without a Host: header (nor X-Forwarded-Host) will throw an error every time I access req.host.

I fixed that to return an empty string.

This also affects req.subdomains.
"".split(".") returns [ "" ].
However, since subdomains slices the initial portion of the host anyway, this will still correctly return []. (unless subdomain offset is 0)

@tj
Copy link
Member

tj commented Apr 15, 2013

cool, just need a test for each if you dont mind! thanks man

@SLaks
Copy link
Author

SLaks commented Apr 16, 2013

Done

@SLaks SLaks mentioned this pull request Apr 16, 2013
@SLaks
Copy link
Author

SLaks commented Apr 16, 2013

I'm not sure whether req.host should return '' or undefined if there is no header.

Thoughts?

@hallas
Copy link

hallas commented Apr 16, 2013

It should definitely be either undefined or null, since the "" (empty string) is an actual value, null is when the reference is there, but it doesn't hold anything, and undefined is when the reference doesn't exists.

I think it should be null.

@SLaks
Copy link
Author

SLaks commented Apr 16, 2013

Good idea.

What about req.subdomains? null or []?

Also, what if the Host: header is present but empty?
I think it should then be "".

@hallas
Copy link

hallas commented Apr 16, 2013

yeah, if it's present but empty, it should be the empty string, its the precise representation, subdomains is fine as an empty array.

@tj
Copy link
Member

tj commented Apr 16, 2013

I'd vote undefined for host and empty array for subdomains

Breaking change; if X-Forwarded-Host: is present but empty, it will no
longer fall back to Host:
@SLaks
Copy link
Author

SLaks commented Apr 16, 2013

Done.

Note that this is a breaking change; if X-Forwarded-Host: is present but empty, it will now return '' rather than falling back to Host:

@@ -472,7 +473,10 @@ req.__defineGetter__('path', function(){
req.__defineGetter__('host', function(){
var trustProxy = this.app.get('trust proxy');
var host = trustProxy && this.get('X-Forwarded-Host');
host = host || this.get('Host');
if (host === false || typeof host === 'undefined') // If X-Forwarded-Host is present but empty, return it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!host) here should be just fine, it shouldn't ever be a boolean and then there's no need for typeof

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh oops read that wrong, this seems a little weird to me still though

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javascript truthiness strikes again!

This allows missing headers, or trustProxy being false.

@SLaks
Copy link
Author

SLaks commented Apr 18, 2013

Is this OK to merge?

@justinpincar
Copy link

+1 please merge. A few search bots don't send the Host: header and it's irritating to have to wrap the getter with error handling.

@@ -445,7 +444,9 @@ req.__defineGetter__('auth', function(){

req.__defineGetter__('subdomains', function(){
var offset = this.app.get('subdomain offset');
return this.get('Host')
var host = this.get('Host');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use this.host here since we already handle it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; in fact, subdomains currently ignores trust proxy completely, which is a bug.

@tj
Copy link
Member

tj commented May 9, 2013

06ead58

@tj tj closed this May 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants